
package com.cptravel.activity.hot;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.core.controller.B5MBaseActivity;
import com.core.util.B5MBitmapHelper;
import com.core.util.Constants;
import com.cptravel.R;
import com.cptravel.bean.Product;
import com.cptravel.view.NoScrollWebView;
import com.cptravel.view.pullscrollview.PullScrollView;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.SendMessageToWX;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.tencent.mm.sdk.openapi.WXMediaMessage;
import com.tencent.mm.sdk.openapi.WXWebpageObject;
import com.tencent.mm.sdk.platformtools.Util;

public class HotDetailHeadViewDescActivity extends B5MBaseActivity
{
    private PullScrollView mPullScrollView;
    private ImageView iv_top;
    private Product product;
    private IWXAPI mIwxapi;

    @Override
    protected String getHeadTitle()
    {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public int doGetContentViewId()
    {
        // TODO Auto-generated method stub
        return R.layout.hot_detail_headview_desc;
    }

    @Override
    public void doInitSubViews(View containerView)
    {
        // TODO Auto-generated method stub
        mPullScrollView = (PullScrollView) findViewById(R.id.pullscrollview);
        iv_top = (ImageView) findViewById(R.id.iv_top);
        mPullScrollView.setHeader(iv_top);
    }

    @Override
    public void doInitDataes()
    {
        // TODO Auto-generated method stub
        product = (Product) getIntent().getSerializableExtra("product");
        TextView tv_score = (TextView) findViewById(R.id.tv_score);
        RatingBar ratingBar = (RatingBar) findViewById(R.id.rb);
        TextView tv_count = (TextView) findViewById(R.id.tv_count);
        TextView tv_address = (TextView) findViewById(R.id.tv_address);
        NoScrollWebView wv_content = (NoScrollWebView) findViewById(R.id.tv_content);
        B5MBitmapHelper.displayDefaultNotPeopleNotRoundImage(product.thum_img, iv_top);
        tv_score.setText(String.format("%s", product.score) + "分");
        ratingBar.setRating(Float.valueOf(product.level));
        tv_count.setText(product.comments + "人");
        tv_address.setText(product.addr);
        wv_content.loadDataWithBaseURL(null, product.content, "text/html", "utf-8", null);
        tv_title.setText(product.title);
        mIwxapi = WXAPIFactory.createWXAPI(mContext, Constants.WEIXIN_APP_ID, false);
    }

    @Override
    public void doAfter()
    {
        // TODO Auto-generated method stub
    }
    private static final int THUMB_SIZE = 150;

    public void sendWeiXin(View view)
    {
        WXWebpageObject webpage = new WXWebpageObject();
        webpage.webpageUrl = "http://laojiabaike.com/";
        WXMediaMessage msg = new WXMediaMessage(webpage);
        msg.title = product.title;
        msg.description = product.content;
        Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ascp);
        Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, THUMB_SIZE, THUMB_SIZE, true);
        bmp.recycle();
        msg.thumbData = Util.bmpToByteArray(thumbBmp, true); // 设置缩略图
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = buildTransaction("temp");
        req.message = msg;
        req.scene = SendMessageToWX.Req.WXSceneSession;
        mIwxapi.sendReq(req);
    }

    private String buildTransaction(final String type)
    {
        return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
    }
}
